<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/22
 * Time: 15:54
 */
namespace app\api\controller;

use think\Db;
use think\Controller;
use think\Exception;
//header("ACCESS-CONTROL-ALLOW-ORIGIN:http://114.215.68.205");

class Agency extends Controller{
	
	private $user;
	
	public function _initialize()
	{
	
		parent::_initialize();
		$map = array(
				'username' => input('username', ''),
				'login_key' => input('login_key', ''),
		);
		$this->user = Db::name('username')->where($map)->where('open', '<', 1)->find();
	}
	public function agentContent(){
		$ret = array(
				'result' => 0,
				'msg' => '',
				'datas' => []
		);
		$query = Db::name('xt_dl')->where('id',1)->value('content');
		$ret['result']=1;
		$ret['datas']=$query;
		return json_encode($ret, JSON_UNESCAPED_UNICODE);
	}
	public function agentContent_app(){
		$query = Db::name('xt_dl')->where('id',1)->value('content');
		return json_encode($query, JSON_UNESCAPED_UNICODE);
	}
	
	/**
	 * 代理报表
	 * @return string
	 */
	public function agentReportForm(){
		$day = input('day','today');
		//user是下级代理名称
		$ret = array(
				'result' => 0,
				'msg' => '',
				'datas' => [
					'one'=>['num'=>0,'chongzhi'=>0,'tixian'=>0,'touzhu'=>0,'tznum'=>0,'cznum'=>0,'zjje'=>0,'yongjin'=>0],
					'two'=>['num'=>0,'chongzhi'=>0,'tixian'=>0,'touzhu'=>0,'tznum'=>0,'cznum'=>0,'zjje'=>0,'yongjin'=>0],
					'tree'=>['num'=>0,'chongzhi'=>0,'tixian'=>0,'touzhu'=>0,'tznum'=>0,'cznum'=>0,'zjje'=>0,'yongjin'=>0],
					'zong'=>['num'=>0,'chongzhi'=>0,'tixian'=>0,'touzhu'=>0,'tznum'=>0,'cznum'=>0,'zjje'=>0,'yongjin'=>0]
				]
		);
		$user = $this->user;
		if(empty($user)){
			$ret['msg'] = '请先登录账号';
			return json_encode($ret, JSON_UNESCAPED_UNICODE);
		}
		$names = Db::name('username')->where('tjr',$user['username'])->column('username');
		foreach ($names as $it){
			$ret['datas']['one']['num'] += 1;
			$cz = Db::name('chongzhi_list')->where(['username'=>$it,'zt'=>1,'czadmin'=>['<>','']])->whereTime('cztime',$day)->value('SUM(je)');
			$ret['datas']['one']['chongzhi'] += intval($cz)?intval($cz):0;
			$ret['datas']['one']['tixian'] += Db::name('tixian_list')->where(['username'=>$it,'zt'=>1,'shadmin'=>['<>',''],'czadmin'=>['<>','']])->whereTime('txtime',$day)->value('SUM(txje)');
			$je = Db::name('touzhu_list_ok')->where('username',$it)->whereTime('tz_time',$day)->value('SUM(tzje)');
			$ret['datas']['one']['touzhu'] += intval($je)?intval($je):0;
			if($je){$ret['datas']['one']['tznum'] += 1;}
			if($cz){$ret['datas']['one']['cznum'] += 1;}
			$zjje = Db::name('touzhu_list_pc')->where('username',$it)->whereTime('tz_time',$day)->value('SUM(zjje)');
			$ret['datas']['one']['zjje'] += intval($zjje)?intval($zjje):0;
			$ret['datas']['one']['yongjin'] += Db::name('agent_balance')->whereTime('add_time',$day)->where('consumer',$it)->value('SUM(balance)');
			$ret['datas']['two']=$this->agentXiajiSum($it, $day);
			$us = Db::name('username')->where('tjr',$it)->column('username');
			foreach ($us as $tm){
				$ret['datas']['tree'] =$this->agentXiajiSum($tm, $day);
			}
		}
		$ret['datas']['zong']['num'] = $ret['datas']['one']['num']+$ret['datas']['two']['num']+$ret['datas']['tree']['num'];
		$ret['datas']['zong']['chongzhi'] = $ret['datas']['one']['chongzhi']+$ret['datas']['two']['chongzhi']+$ret['datas']['tree']['chongzhi'];
		$ret['datas']['zong']['tixian'] = $ret['datas']['one']['tixian']+$ret['datas']['two']['tixian']+$ret['datas']['tree']['tixian'];
		$ret['datas']['zong']['touzhu'] = $ret['datas']['one']['touzhu']+$ret['datas']['two']['touzhu']+$ret['datas']['tree']['touzhu'];
		$ret['datas']['zong']['tznum'] = $ret['datas']['one']['tznum']+$ret['datas']['two']['tznum']+$ret['datas']['tree']['tznum'];
		$ret['datas']['zong']['cznum'] = $ret['datas']['one']['cznum']+$ret['datas']['two']['cznum']+$ret['datas']['tree']['cznum'];
		$ret['datas']['zong']['zjje'] = $ret['datas']['one']['zjje']+$ret['datas']['two']['zjje']+$ret['datas']['tree']['zjje'];
		$ret['datas']['zong']['yongjin'] = $ret['datas']['one']['yongjin']+$ret['datas']['two']['yongjin']+$ret['datas']['tree']['yongjin'];
		$ret['result'] = 1;
		return json_encode($ret, JSON_UNESCAPED_UNICODE);
	}
	private function agentXiajiSum($user,$day){
		$ret = ['num'=>0,'chongzhi'=>0,'tixian'=>0,'touzhu'=>0,'tznum'=>0,'cznum'=>0,'zjje'=>0,'yongjin'=>0];
		$us = Db::name('username')->where('tjr',$user)->column('username');
		foreach ($us as $it){
			$ret['num'] += 1;
			$cz = Db::name('chongzhi_list')->where(['username'=>$it,'zt'=>1,'czadmin'=>['<>','']])->whereTime('cztime',$day)->value('SUM(je)');
			$ret['chongzhi'] += intval($cz)?intval($cz):0;
			$ret['tixian'] += Db::name('tixian_list')->where(['username'=>$it,'zt'=>1,'shadmin'=>['<>',''],'czadmin'=>['<>','']])->whereTime('txtime',$day)->value('SUM(txje)');
			$je = Db::name('touzhu_list_ok')->where('username',$it)->whereTime('tz_time',$day)->value('SUM(tzje)');
			$ret['touzhu'] += intval($je)?intval($je):0;
			if($je){$ret['tznum'] += 1;}
			if($cz){$ret['cznum'] += 1;}
			$zjje = Db::name('touzhu_list_pc')->where('username',$it)->whereTime('tz_time',$day)->value('SUM(zjje)');
			$ret['zjje'] += intval($zjje)?intval($zjje):0;
			$ret['yongjin'] += Db::name('agent_balance')->whereTime('add_time',$day)->where('consumer',$it)->value('SUM(balance)');
			//$this->agentXiajiSum($it,$day);
		}
		return $ret;
	}
    
    
    /**
     * 下级报表
     * @return string
     */
    public function lowerLevelReport(){
    	$name = input('user','');
    	$ret = array(
    			'result' => 0,
    			'msg' => '',
    			'datas' => []
    	);
    	$user = $this->user;
    	if(empty($user)){
    		$ret['msg'] = '请先登录账号';
    		return json_encode($ret, JSON_UNESCAPED_UNICODE);
    	}
    	if($name){
    		$agents = Db::name('username')->field('username,dl,fandian,xjrens,0 as balance')->where('tjr',$name)->select();
    	}else{
    		$agents = Db::name('username')->field('username,dl,fandian,xjrens,0 as balance')->where('tjr',$user['username'])->select();
    	}
    	foreach ($agents as &$val){
    		$val['dl'] = '代理';
    		$yongjin = Db::name('agent_balance')->where('agent',$val['username'])->value('SUM(balance)');
    		$val['balance'] = sprintf('%.2f',$yongjin);
    	}
    	unset($val);
        $ret['result'] = 1;
        $ret['datas'] = $agents;
        return json_encode($ret,JSON_UNESCAPED_UNICODE);
    }
    /**
     * 交易明细
     * @return string
     */
    public function bettingDetail(){
    	//今天：today，昨天：yesterday，本周：week，本月：month，上月：last month
    	$day = input('day','today');
    	$name = input('user','');
    	$ret = array(
    			'result' => 0,
    			'msg' => '',
    			'datas' => ['no_price'=>[],'open_price'=>[]]
    	);
    	$user = $this->user;
    	if(empty($user)){
    		$ret['msg'] = '请先登录账号';
    		return json_encode($ret, JSON_UNESCAPED_UNICODE);
    	}
        $query = Db::name('username')->where('username',$name)->find();
        if($query['nbzh']==1){
            $sql = 'touzhu_list_sw';
        }else{
            $sql = 'touzhu_list_ok';
        }
    	/*if($name!=''){
    		$tzmx = Db::name('username')->field('username')->where('tjr',$name)->select();
    	}else{
    		$tzmx = Db::name('username')->field('username')->where('tjr',$user['username'])->select();
    	}
    	$us = '';
    	foreach ($tzmx as $v){
    		$us .=$v['username'].',';
    	}*/
    	$ret['result']=1;
    	//foreach ($tzmx as $val){
	    	//未中奖
    	$no_perice = Db::name($sql)
    					->alias('tz')
    					->field("tz.username,tz.zjje,tz.tzzs,tz.tzje,'未中奖' as type,tz.tz_time")
    					->join('__CAIPAI_LIST__ cp','cp.id=tz.czid')
    					->field('cp.cp_name_old,cp.cp_name_new')
    					->join('__PLAY_CLASSIFY__ pc','pc.id=tz.wfid')
    					->field('pc.class_name_mini as mini')
    					->where('tz.username',$name)
    				//	->where('tz.username','IN',$us)
    					->whereTime('tz.tz_time',$day)->where('tz.zjje','0')->select();
    	if(!$ret['datas']['no_price']){
    		$ret['datas']['no_price'] = $no_perice;
    	}else{
    		$ret['datas']['no_price'] = array_merge($ret['datas']['no_price'],$no_perice);
    	}
    	array_multisort(array_column($ret['datas']['no_price'],'tz_time'),SORT_DESC,$ret['datas']['no_price'] );
    	//已中奖
    	$open_price = Db::name($sql)
    					->alias('tz')
    					->field("tz.username,tz.zjje,tz.tzzs,tz.tzje,'已中奖' as type,tz.tz_time")
    					->join('__CAIPAI_LIST__ cp','cp.id=tz.czid')
    					->field('cp.cp_name_old,cp.cp_name_new')
    					->join('__PLAY_CLASSIFY__ pc','pc.id=tz.wfid')
    					->field('pc.class_name_mini as mini')
				    	->where('tz.username',$name)
				    //	->where('tz.username','IN',$us)
				    	->whereTime('tz.tz_time',$day)
				    	->where('zjje','>','0')->select();
    	
    	$ret['datas']['open_price'] = $open_price;
    	
    	array_multisort(array_column($ret['datas']['open_price'],'tz_time'),SORT_DESC,$ret['datas']['open_price'] );
	   //}
    	//全部
    	$ret['datas']['all'] = array_merge($ret['datas']['no_price'],$ret['datas']['open_price']);
    	array_multisort(array_column($ret['datas']['all'],'tz_time'),SORT_DESC,$ret['datas']['all'] );
    	$caipiao = Db::name('caipai_list')->field('id,cp_name_old,cp_name_new')->select();
    	$wf = Db::name('play_classify')->select();
    	
    	
    	return json_encode($ret, JSON_UNESCAPED_UNICODE);
    }
    
    public function transactionDetails(){
    	//今天：today，昨天：yesterday，本周：week，本月：month，上月：last month
    	$day = input('day','today');
    	$name = input('user','');
    	$ret = array(
    			'result' => 0,
    			'msg' => '',
    			'datas' => ['chongzhi'=>[],'tixian'=>[]]
    	);
    	$user = $this->user;
    	if(empty($user)){
    		$ret['msg'] = '请先登录账号';
    		return json_encode($ret, JSON_UNESCAPED_UNICODE);
    	}
    	/*$us = '';
    	if($name!=''){
    		$tzmx = Db::name('username')->field('username')->where('tjr',$name)->select();
    	}else{
    		$tzmx = Db::name('username')->field('username')->where('tjr',$user['username'])->select();
    	}
    	foreach ($tzmx as $v){
    		$us .=$v['username'].',';
    	}
    	$ret['result']=1;
    	//充值
    	foreach ($tzmx as $val){*/
    	$chongzhi = Db::name('chongzhi_list')->field('username,je,cztime as time,zt,czadmin,1 as type')->where('username',$name)->where('zt',1)
        //->where('username','IN',$us)
        ->whereTime('cztime',$day)->select();
    	if(!$ret['datas']['chongzhi']){
    		$ret['datas']['chongzhi'] = $chongzhi;
    	}else{
    		$ret['datas']['chongzhi'] = array_merge($ret['datas']['chongzhi'],$chongzhi);
    	}
    	array_multisort(array_column($ret['datas']['chongzhi'],'time'),SORT_DESC,$ret['datas']['chongzhi'] );
    	//提现
    	$tixian = Db::name('tixian_list')->field('username,txje as je,txtime as time,zt,shadmin as czadmin,2 as type')->where('username',$name)->where('zt',1)
       // ->where('username','IN',$us)
        ->whereTime('txtime',$day)->select();
    	if(!$ret['datas']['tixian']){
    		$ret['datas']['tixian'] = $tixian;
    	}else{
    		$ret['datas']['tixian'] = array_merge($ret['datas']['tixian'],$tixian);
    	}
    	array_multisort(array_column($ret['datas']['chongzhi'],'time'),SORT_DESC,$ret['datas']['chongzhi'] );
    	//}
    	//全部
        //
    	$ret['result'] =1;
        $ret['datas']['all'] = array_merge($ret['datas']['chongzhi'],$ret['datas']['tixian']);
    	array_multisort(array_column($ret['datas']['all'],'time'),SORT_DESC,$ret['datas']['all'] );
    	
    	return json_encode($ret, JSON_UNESCAPED_UNICODE);
    }
   

}